/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright (c) 2013-2014 sagyf Yang. The Four Group.
 */

$(function () {

    // Preload images
    $.preloadCssImages();


    // CSS tweaks
    var $header = $('#header');
    $header.find('#nav li:last').addClass('nobg');
    $('.block_head ul').each(function () {
        $('li:first', this).addClass('nobg');
    });
    $('.block form input[type=file]').addClass('file');


    // Sort table
    $("table.sortable").tablesorter({
        headers: { 0: { sorter: false}, 5: {sorter: false} },		// Disabled on the 1st and 6th columns
        widgets: ['zebra']
    });

    $('.block table tr th.header').css('cursor', 'pointer');


    // Check / uncheck all checkboxes
    $('.check_all').click(function () {
        $(this).parents('form').find('input:checkbox').attr('checked', $(this).is(':checked'));
    });


    // Messages
    $('.block .message').hide().append('<span class="close" title="Dismiss"></span>').fadeIn('slow');
    var $block = $('.block .message .close');
    $block.hover(
        function () {
            $(this).addClass('hover');
        },
        function () {
            $(this).removeClass('hover');
        }
    );

    $block.click(function () {
        $(this).parent().fadeOut('slow', function () {
            $(this).remove();
        });
    });

    jQuery.extend(DateInput.DEFAULT_OPTS, {
        month_names      : ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"],
        short_month_names: ["一", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "十二"],
        short_day_names  : ["一", "二", "三", "四", "五", "六", "日"]
    });
    $.extend(DateInput.DEFAULT_OPTS, {
        stringToDate: function (string) {
            var matches;
            if (matches = string.match(/^(\d{4,4})-(\d{2,2})-(\d{2,2})$/)) {
                return new Date(matches[1], matches[2] - 1, matches[3]);
            } else {
                return null;
            }
        },

        dateToString: function (date) {
            var month = (date.getMonth() + 1).toString();
            var dom = date.getDate().toString();
            if (month.length == 1) month = "0" + month;
            if (dom.length == 1) dom = "0" + dom;
            return date.getFullYear() + "-" + month + "-" + dom;
        }
    });
    // Date picker
    $('input.date_picker').date_input();


    // Navigation dropdown fix for IE6
    if (jQuery.browser.version.substr(0, 1) < 7) {
        $header.find('#nav li').hover(
            function () {
                $(this).addClass('iehover');
            },
            function () {
                $(this).removeClass('iehover');
            }
        );
    }


    // IE6 PNG fix
    $(document).pngFix();

});